﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NK.Mvc3.Models;
using NK.Mvc3.DBL;

namespace NK.Mvc3.Controllers
{
    public class SaleDetailAjaxGridController : Controller
    {
        private StoreContext db = new StoreContext();

        //
        // GET: /SaleDetailAjaxGrid/

        public ViewResult Index(int start = 0, int itemsPerPage = 20, string orderBy = "ID", bool desc = false)
        {
            ViewBag.Count = db.SaleDetails.Count();
            ViewBag.Start = start;
            ViewBag.ItemsPerPage = itemsPerPage;
            ViewBag.OrderBy = orderBy;
            ViewBag.Desc = desc;

            return View();
        }

        public PartialViewResult Grid(int start = 0, int itemsPerPage = 20, string orderBy = "ID", bool desc = false)
        {
            ViewBag.Count = db.SaleDetails.Count();
            ViewBag.Start = start;
            ViewBag.ItemsPerPage = itemsPerPage;
            ViewBag.OrderBy = orderBy;
            ViewBag.Desc = desc;
            return PartialView();
        }
        //
        // GET: /SaleDetailAjaxGrid/GridData/?start=0&itemsPerPage=20&orderBy=ID&desc=true

        public ActionResult GridData( int start = 0, int itemsPerPage = 20, string orderBy = "ID", bool desc = false)
        {
            //ViewBag.SaleID = saleId;
            Response.AppendHeader("X-Total-Row-Count", db.SaleDetails.Count().ToString());
            ObjectQuery<SaleDetail> saledetails = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<SaleDetail>();
            //ObjectQuery<SaleDetail> saledetails = db.SaleDetails.Where(s = > s.SaleID == saleId)
            saledetails = (ObjectQuery<SaleDetail>)saledetails.Include(s => s.ProductInfo).Include(s => s.Sale);
            //saledetails = saledetails.Include("it.ProductInfo").Include("it.SaleDetail");
            saledetails = saledetails.OrderBy("it." + orderBy + (desc ? " desc" : ""));
            //var a = db.StoreInputs.OrderBy(s => s.ID);

            return PartialView(saledetails.Skip(start).Take(itemsPerPage));
        }

        //
        // GET: /Default5/RowData/5

        public ActionResult RowData(int id)
        {
            SaleDetail saledetail = db.SaleDetails.Find(id);
            return PartialView("GridData", new SaleDetail[] { saledetail });
        }

        //
        // GET: /SaleDetailAjaxGrid/Create

        public ActionResult Create()
        {
            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group");
            ViewBag.SaleID = new SelectList(db.Sales, "ID", "StaffName");
            return PartialView("Edit");
        }

        //
        // POST: /SaleDetailAjaxGrid/Create

        [HttpPost]
        public ActionResult Create(SaleDetail saledetail)
        {
            if (ModelState.IsValid)
            {
                db.SaleDetails.Add(saledetail);
                db.SaveChanges();
                return PartialView("GridData", new SaleDetail[] { saledetail });
            }

            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group", saledetail.ProductInfoID);
            ViewBag.SaleID = new SelectList(db.Sales, "ID", "StaffName", saledetail.SaleID);
            return PartialView("Edit", saledetail);
        }

        //
        // GET: /SaleDetailAjaxGrid/Edit/5

        public ActionResult Edit(int id)
        {
            SaleDetail saledetail = db.SaleDetails.Find(id);
            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group", saledetail.ProductInfoID);
            ViewBag.SaleID = new SelectList(db.Sales, "ID", "StaffName", saledetail.SaleID);
            return PartialView(saledetail);
        }

        //
        // POST: /SaleDetailAjaxGrid/Edit/5

        [HttpPost]
        public ActionResult Edit(SaleDetail saledetail)
        {
            if (ModelState.IsValid)
            {
                db.Entry(saledetail).State = EntityState.Modified;
                db.SaveChanges();
                return PartialView("GridData", new SaleDetail[] { saledetail });
            }

            ViewBag.ProductInfoID = new SelectList(db.ProductInfos, "ID", "Group", saledetail.ProductInfoID);
            ViewBag.SaleID = new SelectList(db.Sales, "ID", "StaffName", saledetail.SaleID);
            return PartialView(saledetail);
        }

        //
        // POST: /SaleDetailAjaxGrid/Delete/5

        [HttpPost]
        public void Delete(int id)
        {
            SaleDetail saledetail = db.SaleDetails.Find(id);
            db.SaleDetails.Remove(saledetail);
            db.SaveChanges();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
